TAG_ROOT_VER=888;
TAG_ROOT_NODE=999;

/**
 * 药品库
 */
Lib = cc.Sprite.extend({
	libArr: [],
	openFlag:false,
	doing:false,
	doright:true,
	ttt:true,
	curPos:0,
	rm : 141.5,
	ctor:function(p){
		this._super();
		p.addChild(this,300,TAG_LIB);
		//添加药品库背景
		this.loadBg();
		//添加遮罩
		this.loadClipping();
		//添加滚动条
		this.loadVer();
		//添加滑动事件
		this.loadSlide();		
	},
	init:function(tags){
		var libShow = this.getChildByTag(TAG_LIBSHOW);
		var clipping = libShow.getChildByTag(TAG_CLIPPING);
		var libMove = clipping.getChildByTag(TAG_LIBMOVE);
		
		this.libArr = [];
		this.openFlag = false;
		var first = true;
		var firstPos = cc.p((libMove.width-50)/12+2,(libMove.height/3*2+30));
		var prev = null;
		for(var i in tags){
		var rel = this.getLibRel(tags[i]);
		var lib = new Angel2(libMove,10, rel.tag, rel.img, this.callback);
		lib.setAnchorPoint(0.5,0);
		lib.setScale(0.6);
		if(first){
			first = false;
			lib.setPosition(firstPos);
		} else {
			lib.right(prev, this.rm)
			}
			prev = lib;
		}
	},
	loadBg:function(){
		var libShow = new cc.LayerColor(cc.color(51,50,50),900,500);
		this.addChild(libShow, 200,TAG_LIBSHOW);
		libShow.setPosition(cc.p(1300,186));
	
		this.menu = new cc.Menu();
		this.closed = new cc.MenuItemImage("#button/closed1.png","#button/closed2.png",this.action,this);
		this.sured = new cc.MenuItemImage("#button/sured1.png","#button/sured2.png",this.action,this);
		libShow.addChild(this.menu, 1,TAG_LIBSHOW_MENU);
		this.menu.setPosition(cc.p(0,0));
		this.menu.addChild(this.closed, 2, TAG_LIB_CLOSED);
		this.closed.setAnchorPoint(0, 0);
		this.closed.setPosition(cc.p(150,22));
		this.menu.addChild(this.sured,2,TAG_LIB_SURED);
		this.sured.setAnchorPoint(0, 0);
		this.sured.setPosition(cc.p(575,22));						
	},
	loadClipping:function(){
		var libShow = this.getChildByTag(TAG_LIBSHOW);
		var stencil = new cc.DrawNode()
		stencil.drawRect(cc.p(22,80),cc.p(864,450),cc.color(51,50,50),0,cc.color(51,50,50));
		var clipping = new cc.ClippingNode(stencil);
		clipping.setInverted(false); 

		libShow.addChild(clipping, 2,TAG_CLIPPING);
		clipping.setStencil(stencil);
		clipping.stencil = stencil;
		
		this.loadLibMove()
	},
	loadLibMove:function(){
		var libShow = this.getChildByTag(TAG_LIBSHOW);
		var clipping = libShow.getChildByTag(TAG_CLIPPING);
		var libMove = new cc.LayerColor(cc.color(83,83,83),842,560);
		clipping.addChild(libMove,3,TAG_LIBMOVE);
		libMove.setPosition(cc.p(22,-112));
		
		var hori_line1 = new cc.LayerColor(cc.color(51, 50, 50),842,10);
		libMove.addChild(hori_line1);
		hori_line1.setPosition(cc.p(0,libMove.height/3*2-5));		
		
		var hori_line3 = new cc.LayerColor(cc.color(51, 50, 50),842,10);
		libMove.addChild(hori_line3);
		hori_line3.setPosition(cc.p(0,libMove.height/3-5));
		
		var vert_line1 = new cc.LayerColor(cc.color(51, 50, 50),10,560);
		libMove.addChild(vert_line1);
		vert_line1.setPosition(cc.p(libMove.width/6-7,0));
		
		var vert_line2 = new cc.LayerColor(cc.color(51, 50, 50),10,560);
		libMove.addChild(vert_line2);
		vert_line2.setPosition(cc.p(libMove.width/6*2-6,0));
		
		var vert_line3 = new cc.LayerColor(cc.color(51, 50, 50),10,560);
		libMove.addChild(vert_line3);
		vert_line3.setPosition(cc.p(libMove.width/6*3-5,0));
		
		var vert_line4 = new cc.LayerColor(cc.color(51, 50, 50),10,560);
		libMove.addChild(vert_line4);
		vert_line4.setPosition(cc.p(libMove.width/6*4-4,0));
		
		var vert_line5 = new cc.LayerColor(cc.color(51, 50, 50),10,560);
		libMove.addChild(vert_line5);
		vert_line5.setPosition(cc.p(libMove.width/6*5-3,0));
	},
	loadVer:function(){
		var libShow = this.getChildByTag(TAG_LIBSHOW);
		// 空心条子
		this.ver = new cc.Sprite("#ver_line.png");
		this.ver.setPosition(this.width+libShow.width-this.ver.width*0.5,this.ver.height*0.5+80);
		libShow.addChild(this.ver, 60,TAG_ROOT_VER);
		this.ver.setCascadeOpacityEnabled(true);
		// 位置点
		this.pos = new cc.Sprite("#sp1.png");
		this.pos.setPosition(this.ver.width * 0.5, this.ver.height-this.pos.height*0.5);
		this.ver.addChild(this.pos, 3,TAG_ROOT_NODE);
		//向上箭头
		var upArrow = new Angel(this.ver, "#sel_up_arrow.png", this.up, this);
		upArrow.setLocalZOrder(4);
		upArrow.setPosition(this.ver.width * 0.5, this.ver.height + 10);
		//向下箭头
		var downArrow = new Angel(this.ver, "#sel_down_arrow.png", this.down, this);
		downArrow.setLocalZOrder(4);
		downArrow.setPosition(this.ver.width * 0.5, -10 );
	},
	up:function(){
		if(this.curPos <= 0){
			return;
		}
		this.curPos=this.curPos-1;
		var pos = this.getChildByTag(TAG_LIBSHOW).getChildByTag(TAG_ROOT_VER).getChildByTag(TAG_ROOT_NODE);
		if(pos.y<358){
			pos.runAction(cc.moveBy(0.6, 0, 370/15));
		}
		//箭头向上，仪器向下
		var libMove = this.getChildByTag(TAG_LIBSHOW).getChildByTag(TAG_CLIPPING).getChildByTag(TAG_LIBMOVE);
		if(libMove.y>-110){
		libMove.runAction(cc.moveBy(0.6, 0,-202/15));
		}
		gg.flow.location();
	},
	down:function(){
		if(this.curPos >=14 ){
			return;
		}
		this.curPos=this.curPos+1;
		var pos = this.getChildByTag(TAG_LIBSHOW).getChildByTag(TAG_ROOT_VER).getChildByTag(TAG_ROOT_NODE);
		if(pos.y>13){
			pos.runAction(cc.moveBy(0.6, 0, -370/15));
		}
		//箭头向下，仪器向上		
		var libMove = this.getChildByTag(TAG_LIBSHOW).getChildByTag(TAG_CLIPPING).getChildByTag(TAG_LIBMOVE);
		if(libMove.y<82){
			libMove.runAction(cc.moveBy(0.6, 0,202/15));
		}	
		gg.flow.location();
	},
	getLibRel:function(tag){
		for(var i in libRelArr){
			if(libRelArr[i].tag == tag){
				return libRelArr[i];
			}
		}
	},
	moveLib:function(tag,width){
		width = 75;
		var begin = false;
		for(var i in this.libArr){
			var libTag = this.libArr[i];
			if(tag == libTag){
				begin = true;
			}
			if(begin){
				var lib = this.getChildByTag(libTag);
				if(lib != null){
					lib.runAction(cc.moveBy(0.5,cc.p(-width, 0)));
				}
			}
		}
	},
	action:function(p){
		var pos = this.getPosition(); 
		var action = gg.flow.flow.action;
		switch(p.getTag()){
			case TAG_LIB_CLOSED:
				ll.run.lib.close();
				ll.tool.lib.setNormalSpriteFrame("#button/lib1.png");
				ll.tool.lib.setSelectedSpriteFrame("#button/lib1.png");
				break;
			case TAG_LIB_SURED:	
				if(this.doright){
				var lib_show = this.getChildByTag(TAG_LIBSHOW).getChildByTag(TAG_CLIPPING).getChildByTag(TAG_LIBMOVE);
				var draw_ampulla = lib_show.getChildByTag(TAG_LIB_AMPULLA).getChildByTag(TAG_DRAW);
				var draw_beaker = lib_show.getChildByTag(TAG_LIB_BEAKER).getChildByTag(TAG_DRAW);
				var draw_conical = lib_show.getChildByTag(TAG_LIB_CONICAL).getChildByTag(TAG_DRAW);
				var draw_cylinder = lib_show.getChildByTag(TAG_LIB_CYLINDER).getChildByTag(TAG_DRAW);
				var draw_testTube = lib_show.getChildByTag(TAG_LIB_TESTTUBE).getChildByTag(TAG_DRAW);
				var draw_volumetric = lib_show.getChildByTag(TAG_LIB_VOLUMETRIC).getChildByTag(TAG_DRAW);
				this.draw = [];
				this.draw.push(draw_beaker,draw_conical,draw_cylinder,draw_testTube,draw_volumetric)				
					for(var i in this.draw){
						if(this.draw[i]!==null){
							if(ll.run.getChildByTag(TAG_SHOW)==null){
								this.showTip = new ShowTip("仪器选择错误！",cc.p(750,370));
								ll.tip.mdScore(-3);
								return;
							}
						}
					}
				if(draw_ampulla!==null){
					if(this.doright){					
						var ampulla = ll.run.ampulla;
						ampulla.setVisible(true);
						gg.flow.next();
						this.doright=false;		
						ll.run.lib.close();
						ll.tool.lib.setNormalSpriteFrame("#button/lib1.png");
						ll.tool.lib.setSelectedSpriteFrame("#button/lib1.png");
						draw_ampulla.removeFromParent();
						ll.tip.mdScore(10);
					}					
				}
				else{
					this.showTip = new ShowTip("请选择仪器！",cc.p(750,370));
					ll.tip.mdScore(-3);
				}					
			}else{
				this.showTip = new ShowTip("仪器已选择，请点击关闭并返回实验！",cc.p(750,370));
			}
				break;
			default:
				break;
		}
//		if(action == ACTION_NONE){
//			this.moveLib(p.getTag(), p.width * p.getScale());
//			p.removeFromParent(true);
//		}
	},
	callback:function(p){
		switch(p.getTag()){
		case TAG_LIB_AMPULLA:
			if(this.doright2==undefined){
				gg.flow.next();	
				this.doright2=false;
			}				
			break;
		case TAG_LIB_BEAKER:
		case TAG_LIB_CONICAL:
		case TAG_LIB_CYLINDER:
		case TAG_LIB_TESTTUBE:
		case TAG_LIB_VOLUMETRIC:

			break;
		}
	},
	isOpen:function(){
		return this.openFlag; 
	},
	open:function(){
		if(this.openFlag|| this.doing){
			return;
		}				
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(-this.getChildByTag(TAG_LIBSHOW).width-100,0));
		var seq = cc.sequence(move,cc.callFunc(function() {
			this.openFlag = true;
			this.doing = false;
			var tag = gg.flow.flow.tag;
				//箭头指向按钮
				gg.flow.location();	
		}, this));
		this.runAction(seq);
	},
	close:function(){
		if(!this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var formula = ll.run.getChildByTag(TAG_SHOW);
		if(formula !== null){
			formula.kill();
		}
		var move = cc.moveBy(0.4, cc.p(this.getChildByTag(TAG_LIBSHOW).width+100,0));
		var func = cc.callFunc(function(){
			this.openFlag = false;
			this.doing = false;
			var tag = gg.flow.flow.tag;
				//箭头指向按钮
				gg.flow.location();

		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	},
	loadSlide:/**
	 * 滑动监听
	 */
		function(){
//		上下滑动监听
		var listener_touch = cc.EventListener.create({
			event: cc.EventListener.TOUCH_ONE_BY_ONE,
			swallowTouches: false,
			hover: false,
			});
		if ('mouse' in cc.sys.capabilities){
			var listener_mouse = cc.EventListener.create({
				event: cc.EventListener.MOUSE,
				swallowTouches: false,
				onMouseScroll:UpperLowerSliding.onMouseScroll
			});
			cc.eventManager.addListener(listener_mouse, this);
		}
	}
});
libRelArr = [
     {tag:TAG_LIB_MIN, name:""},
     {tag:TAG_LIB_AMPULLA, name:"玻璃瓶",img:"res/svg/ampulla.svg"},
     {tag:TAG_LIB_BEAKER, name:"烧杯",img:"res/svg/beaker.svg"},
     {tag:TAG_LIB_CONICAL, name:"锥形瓶",img:"res/svg/conical.svg"},
     {tag:TAG_LIB_CYLINDER, name:"量筒",img:"res/svg/cylinder.svg"},
     {tag:TAG_LIB_TESTTUBE, name:"试管",img:"res/svg/testTube.svg"},
     {tag:TAG_LIB_VOLUMETRIC, name:"长颈试瓶",img:"res/svg/volumetric.svg"},
//     {tag:TAG_LIB_IRON, name:"铁架",img:"#ironShelf.png"},
//     {tag:TAG_LIB_FUNNEL, name:"漏斗",img:"#funnel.png"},
//     {tag:TAG_LIB_ROD, name:"玻璃棒",img:"#rod.png"},
     ];

